﻿#include "computer.h"
#include "ui_computer.h"
#include "state.h"
using namespace std;

computer::computer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::computer)
{
    ui->setupUi(this);
    this->setFixedSize(1000,650);
    this->setWindowTitle("计算机移动");

    start = new QPushButton(this);
    title = new QLabel(this);
    backgame = new QPushButton(this);

    start->setGeometry(700,100,250,75);
    backgame->setGeometry(700,200,250,75);
    title->setGeometry(135,20,250,75);


    title->setFont(QFont("方正楷体",18));
    title->setText("计算机移动");
    start->setFont(QFont("方正楷体",16));
    backgame->setFont(QFont("方正楷体",16));

    start->setText("开始");
    backgame->setText("返回");
    start->setStyleSheet("border: 4px inset rgb(255,255,255);""border-radius:10px;");
    start->setFocusPolicy(Qt::NoFocus);//NoFocus
    backgame->setStyleSheet("border: 4px inset rgb(255,255,255);""border-radius:10px;");
    backgame->setFocusPolicy(Qt::NoFocus);//NoFocus
    connect(start, &QPushButton::clicked, this, &computer::automove);

    QImage *backImg = new QImage();
    backImg->load(":/Image/resource/backImg.png");
    QLabel *back = new QLabel(this);
    back->setFixedSize(1000,650);
    back->setPixmap(QPixmap::fromImage(*backImg));
    back->lower();
    blockAudio = new QSound(":/sound/resource/BlockAudio.wav");
}

void computer::layoutbutton(int *p,int N){
    qDebug()<<N;
    this->N = N;
    newblock = new QPushButton[N];
    const int a=75;
    const int b=100;
    const int side=100;
    int sqn = sqrt(N);
    for(int i=0;i<sqn;i++){
        for(int j=0;j<sqn;j++){
            //将按钮控件添加到当前的窗口中
            newblock[sqn*i+j].setParent(this);
            //(左边缘，上边缘，宽，高)设置大小
            newblock[sqn*i+j].setGeometry(a+side*j,b+side*i,side,side);
            newblock[sqn*i+j].setFont(QFont("HGB8X_CNKI",18));
            newblock[sqn*i+j].setStyleSheet("background-image: url(:/Image/resource/blockImg.jpg);");
            newblock[sqn*i+j].setFocusPolicy(Qt::NoFocus);//NoFocus

            if(!(i == sqn-1 && j == sqn-1))
                newblock[sqn*i+j].setText(QString::number(p[sqn*i+j]));
        }
    }
}



void computer::automove(){
    // 获取当前游戏状态，并转换为solve函数所需的格式
   string current_state = "";
   for (int i = 0; i < N-1; ++i) {
       int temp = stoi(newblock[i].text().toStdString());
       switch (temp) {
       case 1:current_state+="1";break;
       case 2:current_state+="2";break;
       case 3:current_state+="3";break;
       case 4:current_state+="4";break;
       case 5:current_state+="5";break;
       case 6:current_state+="6";break;
       case 7:current_state+="7";break;
       case 8:current_state+="8";break;
       case 9:current_state+="9";break;
       case 10:current_state+="A";break;
       case 11:current_state+="B";break;
       case 12:current_state+="C";break;
       case 13:current_state+="D";break;
       case 14:current_state+="E";break;
       case 15:current_state+="F";break;
       case 16:current_state+="G";break;
       case 17:current_state+="H";break;
       case 18:current_state+="I";break;
       case 19:current_state+="J";break;
       case 20:current_state+="K";break;
       case 21:current_state+="L";break;
       case 22:current_state+="M";break;
       case 23:current_state+="N";break;
       case 24:current_state+="O";break;
       }
   }
    current_state +="x";
   // 调用solve函数获取解决方案
    solution = solve(current_state, N);

    QObject::connect(&timer, &QTimer::timeout, this, [this]()
    {
        if (currentIndex < solution.size())
        {
           const std::string& move = solution[currentIndex++];
           // 执行移动操作
           if (move == "up")
           {
               newblock[currentBlockPosition].setText(newblock[currentBlockPosition-3].text());
               newblock[currentBlockPosition-3].setText("");
               currentBlockPosition=currentBlockPosition-3;
               blockAudio->play();
           }
           else if (move == "down")
           {
               newblock[currentBlockPosition].setText(newblock[currentBlockPosition+3].text());
               newblock[currentBlockPosition+3].setText("");
               currentBlockPosition=currentBlockPosition+3;
               blockAudio->play();

           }
           else if (move == "left")
           {
               newblock[currentBlockPosition].setText(newblock[currentBlockPosition-1].text());
               newblock[currentBlockPosition-1].setText("");
               currentBlockPosition=currentBlockPosition-1;
               blockAudio->play();

           }
           else if (move == "right")
           {
               newblock[currentBlockPosition].setText(newblock[currentBlockPosition+1].text());
               newblock[currentBlockPosition+1].setText("");
               currentBlockPosition=currentBlockPosition+1;
               blockAudio->play();

           }
        }
        if (currentIndex == solution.size())
        {
             timer.stop();
             QMessageBox overWindow;
             overWindow.setWindowTitle("计算机玩家");
             overWindow.setText("最少步骤解决方案完成！");
             overWindow.setFixedSize(100,65);
             overWindow.exec();
        }
        });
       timer.start(500);
}

computer::~computer()
{
    delete ui;
}
